package com.nbsaas.boot.sql;

import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;

public class SqlUtils {


    public static String clearOrderBy(String sql) {
        try {
            Statement statement = CCJSqlParserUtil.parse(sql);
            if (statement instanceof Select select) {
                PlainSelect plainSelect = (PlainSelect) select;

                // 去掉 ORDER BY
                plainSelect.setOrderByElements(null);

                System.out.println("去掉排序后的SQL:");
                return select.toString();
            }
        }catch (Exception ignored){}

        return sql;
    }
}
